#include "eventshandler.h"
#include <QMessageBox>
#include <QFile>
#include <QTextStream>

EventsHandler::EventsHandler() : First(NULL)
{
    loadList();
}

EventList* EventsHandler::FindEventsByDate(QDate date)
{
    EventList *temp, *newList, *temp2;
    temp = First;
    newList = NULL;

    while (temp)
    {
        if (temp->Data.dateTime.date() == date)
        {
            if (!newList)
            {
                newList = new EventList();
                newList->Data = temp->Data;
            }
            else
            {
                temp2 = newList;
                while (temp2->next)
                    temp2 = temp2->next;
                temp2->next = new EventList();
                temp2->next->Data = temp->Data;
            }
        }
        temp = temp->next;
    }
    return newList;
}

void EventsHandler::AddEvent(QDate date, QTime time, QString descr)
{
    EventList *tempList;
    tempList = First;
    Event dt;
    dt.dateTime.setDate(date);
    dt.description=descr;
    dt.dateTime.setTime(time);

    if (!First)
    {
        First = new EventList();
        First->Data = dt;
    }
    else
    {
        while(tempList->next)
            tempList = tempList->next;

        tempList->next = new EventList();
        tempList->next->Data = dt;
    }
    saveList();
}

void EventsHandler::AddEvent(Event dt)
{
    EventList *tempList;
    tempList = First;

    if (!First)
    {
        First = new EventList();
        First->Data = dt;
    }
    else
    {
        while(tempList->next)
            tempList = tempList->next;

        tempList->next = new EventList();
        tempList->next->Data = dt;
    }
    saveList();
}

void EventsHandler::showList()
{
    EventList *tmp;
    tmp=First;

    if (tmp)
        QMessageBox::warning(0,tmp->Data.dateTime.toString("yyyy.MM.dd-HH:mm:ss"),tmp->Data.description);

    while (tmp->next)
    {
        tmp = tmp->next;
        QMessageBox::warning(0,tmp->Data.dateTime.toString("yyyy.MM.dd-HH:mm:ss"),tmp->Data.description);
    }
}

void EventsHandler::saveList()
{
    QFile f;
    f.setFileName("data.dat");
    if(!f.open(QIODevice::WriteOnly))
    {
        QMessageBox::warning(0,"Failed","We failed to open data.dat file for writting.");
        return;
    }
    QTextStream ts( &f );
    EventList *tmp;
    tmp = First;
    while (tmp)
    {
        ts << tmp->Data.dateTime.toString("yyyy.MM.dd-HH:mm:ss") << " " << tmp->Data.alarmDateTime.toString("yyyy.MM.dd-HH:mm:ss") << " " << tmp->Data.alarmTime.toString() << " " << tmp->Data.description.simplified() << endl;
        tmp = tmp->next;
    }
    //f.write()
    f.close();
}

void EventsHandler::loadList()
{
    Event temp;
    QString tbr;
    QFile f("data.dat");
    if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QMessageBox::warning(0, "Failed", "We failed to open data.dat file for reading.");
        return;
    }
    QTextStream strm(&f);
    while (!strm.atEnd())
    {
        strm >> tbr;
        strm.skipWhiteSpace();
        temp.dateTime = QDateTime::fromString(tbr,"yyyy.MM.dd-HH:mm:ss");
        strm >> tbr;
        strm.skipWhiteSpace();
        temp.alarmDateTime = QDateTime::fromString(tbr,"yyyy.MM.dd-HH:mm:ss");
        strm.skipWhiteSpace();
        strm >> tbr;
        temp.alarmTime = QTime::fromString(tbr, "hh:mm:ss");
        //QMessageBox::warning(0,"Failed",temp.date.toString("yyyy.MM.dd-HH:mm:ss"));
        strm.skipWhiteSpace();
        temp.description = strm.readLine();
        AddEvent(temp);
    }
    f.close();
}

int EventsHandler::GetAllDates(QDateTime A[])
{
    int n = 0;
    EventList *tmp;
    tmp = First;

    while (tmp)
    {
        A[n++] = tmp->Data.dateTime;
        tmp = tmp->next;
    }
    return n;
}

int EventsHandler::GetAllAlarmTimes(QDateTime A[])
{
    int n = 0;
    EventList *tmp;
    tmp = First;

    while (tmp)
    {
        if (tmp->Data.alarmDateTime > QDateTime::currentDateTime())
            A[n++] = tmp->Data.alarmDateTime;
        tmp = tmp->next;
    }
    return n;
}

Event* EventsHandler::GetEvent(Event A)
{
    EventList *tmp;
    tmp = First;

    while (tmp)
    {
        if (tmp->Data.dateTime.time() == A.dateTime.time() && tmp->Data.description == A.description && tmp->Data.dateTime.date() == A.dateTime.date())
            return &tmp->Data;
        tmp = tmp->next;
    }
    return NULL;
}

Event EventsHandler::GetEvent(QDateTime alarmTime)
{
    EventList *tmp;
    tmp = First;
    Event A;

    while (tmp)
    {
        if (tmp->Data.alarmDateTime == alarmTime)
            return tmp->Data;
        tmp = tmp->next;
    }
    QMessageBox::warning(0, "Error!", "This MUST NOT HAPPEN! GetEvent by alarm time FAILED!");
    return A;
}

void EventsHandler::RemoveEvent(Event *A)
{
    EventList *tmp,*tmp2;
    tmp = First;

    if ((int)First == (int)A)
    {
        tmp2 = First;
        First = First->next;
        delete tmp2;
        return;
    }

    while (tmp)
    {
        if ((int)tmp->next == (int)A)
        {
            tmp2 = tmp->next;
            tmp->next = tmp->next->next;
            delete tmp2;
            continue;
        }
        tmp = tmp->next;
    }
    saveList();
}

void Event::editEvent(Event *A, Event B)
{
    A->alarmDateTime = B.alarmDateTime;
    A->dateTime = B.dateTime;
    A->description = B.description;
    A->alarmTime = B.alarmTime;
}
